#include "llt_v4chksum.hh"

using namespace userFunction;

void
v4chksum::processData(lee::PktData * const pkt)
{

  tt::Packet * v4pkt;
  v4pkt = static_cast<tt::Packet *>(pkt);
  uint16_t word16;
  uint32_t sum=0;

  v4pkt->ipHdr.check = 0;
  uint16_t * buff = (uint16_t *)&(v4pkt->ipHdr);
    
  // make 16 bit words out of every two adjacent 8 bit words in the packet
  // and add them up
  for (uint16_t i=0;i<20;i=i+2){
    word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
    sum = sum + (uint32_t) word16;	
  }
	
  // take only 16 bits out of the 32 bit sum and add up the carries
  while (sum>>16) {
    sum = (sum & 0xFFFF)+(sum >> 16);
  }

  // one's complement the result
  sum = ~sum;

  // store checksum in the header
  v4pkt->ipHdr.check = sum;

  normal(pkt);
	
}
